package com.sunspring.jbug.service;

import com.google.common.io.Files;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.Instant;

/**
 * @Author: wulei
 * @Date: 2019/3/6
 */
@Service
public class FileService {

    @Value("${sunSpring.static.file.path}")
    private String localFilePath;

    public String save(MultipartFile multipartFile) throws IOException {
        if (multipartFile != null) {
            String realFileName = Instant.now().getEpochSecond() + multipartFile.getOriginalFilename();
            File file = new File(localFilePath + realFileName);
            if (!file.exists()) {
                //Files.write(multipartFile.getBytes(),file);
                multipartFile.transferTo(file);
                return realFileName;
            }
        }
        return null;
    }

}
